import os
import tool as tl
import csv

def reb(oldpath, newpath):
    old_csvnames = os.listdir(oldpath)
    for name in old_csvnames:
        new_storecsv = newpath + name
        writer_csv = csv.writer(open(new_storecsv, 'w+', newline=''))
        newstr = tl.read_csv(oldpath + name)[0:8]
        writer_csv.writerow(newstr)

def get_dirances(array):
    l = len(array)
    res = []
    for i in range(l):
        c = 0
        for j in range(i, l):
            c = c + array[j]
        res.append(c)
    return res


def reb2(oldpath, newpath):
    old_csvnames = os.listdir(oldpath)
    for name in old_csvnames:
        new_storecsv = newpath + name
        writer_csv = csv.writer(open(new_storecsv, 'w+', newline=''))
        newstr1 = tl.to_float_dim1(tl.read_csv(oldpath + name)[1][0:8])
        newstr2 = get_dirances(newstr1)
        newstr2.append(0.0)
        writer_csv.writerow(newstr2)
        writer_csv.writerow(newstr1)

old_path = 'weights_params/IE demo val loss/'
new_path = 'weights_params/IE demo val loss new/'
if not os.path.exists(new_path):
    os.makedirs(new_path)

oldnames = os.listdir(old_path)
for name in oldnames:
    if not 'cnn kd cnn' in name:
        continue
    oldnamepath = old_path + name + '/'
    oldaccpath = oldnamepath + 'acc/'
    oldlosspath = oldnamepath + 'loss/'
    oldresultpath = oldnamepath + 'result/'

    newnamepath = new_path + name + '/'
    newaccpath = newnamepath + 'acc/'
    newlosspath = newnamepath + 'loss/'
    newresultpath = newnamepath + 'result/'
    if not os.path.exists(newnamepath):
        os.makedirs(newnamepath)
        os.makedirs(newaccpath)
        os.makedirs(newlosspath)
        os.makedirs(newresultpath)

    reb(oldaccpath, newaccpath)
    reb(oldlosspath, newlosspath)
    reb2(oldresultpath, newresultpath)


